<?php
class Server {
  public static function submit_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    $orm->update('pb_ecom_order', $req);
  }
  
  public static function pay($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    $orm->update('pb_ecom_order', $req);
    require_once '../include/ecomutils.class.php';
    EComUtils::update_customer($orm, $req);
  }
  
  public static function cancel($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    $orm->update('pb_ecom_order', $req);
    require_once '../include/ecomutils.class.php';
    EComUtils::update_customer($orm, $req);
    $opts = new stdClass();
    $opts->cd = 't.order_id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $rslt = $orm->get('pb_ecom_purchase', NULL, NULL, NULL, NULL, $opts)->d;
    foreach ($rslt as $itm) {
      EComUtils::update_stock($orm, $itm, 1);
    }
  }
  
  public static function restore($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    require_once '../include/ecomutils.class.php';
    $opts = new stdClass();
    $opts->cd = 't.order_id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $rslt = $orm->get('pb_ecom_purchase', NULL, NULL, NULL, NULL, $opts)->d;
    foreach ($rslt as $itm) {
      if (EComUtils::check_stock($orm, $itm) == false) {
        echo 'Out of stock';
        return;
      }
    }
    
    foreach ($rslt as $itm) {
      EComUtils::update_stock($orm, $itm, 0);
    }
    EComUtils::update_customer($orm, $req);
    $orm->update('pb_ecom_order', $req);
  }
  
  public static function get_objs($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));

    $opts = new stdClass();
    if (isset($req->search->type) && isset($req->search->phrase)) {
      if ($req->search->type == 0) {
        $opts->cd = 't.is_cancel = :v0';
        $opts->cdv = array(':v0' => 0);
      } else if ($req->search->type == 1) {
        $opts->cd = 't.is_paid = :v1 AND t.is_cancel = :v0';
        $opts->cdv = array(':v1' => 0, ':v0' => 0);
      } else if ($req->search->type == 2) {
        $opts->cd = 't.is_paid = :v1 AND ship_date = :v2 AND t.is_cancel = :v0';
        $opts->cdv = array(':v1' => 1, ':v2' => '0000-00-00', ':v0' => 0);
      } else if ($req->search->type == 3) {
        $opts->cd = 't.ship_date >= :v1 AND t.ship_date != :v2 AND t.is_cancel = :v0';
        $opts->cdv = array(':v1' => date('Y-m-d', time() - (86400 * 10)), ':v2' => '0000-00-00', ':v0' => 0);
      } else if ($req->search->type == 4) {
        $opts->cd = 't.ship_date < :v1 AND t.ship_date != :v2 AND t.is_cancel = :v0';
        $opts->cdv = array(':v1' => date('Y-m-d', time() - (86400 * 10)), ':v2' => '0000-00-00', ':v0' => 0);
      } else if ($req->search->type == 5) {
        $opts->cd = 't.is_cancel = :v0';
        $opts->cdv = array(':v0' => 1);
      }
      
      $opts->cd .=  ' AND (t.order_number LIKE :v3 OR t.first_name LIKE :v3 OR t.last_name LIKE :v3 OR t.phone LIKE :v3 OR t.email LIKE :v3)';
      $opts->cdv[":v3"] = '%' . $req->search->phrase . '%';
    }
    
    $res = $orm->get('pb_ecom_order', $req->o, $req->q, $req->pg, SIZE, $opts);
    echo json_encode($res);
  }
}
?>